<?php
class BillableItem {
	protected $serviceId;
	protected $serviceName;
	protected $quantity;
	protected $unit;
	protected $modifiedPricePerUnit = 0;
    protected $normalFee = 0;
    protected $insuranceFee  = 0;
    protected $insuranceType = 0;
    protected $insuranceRenumeration = 0;

	protected $originalPerUnit  = 0;
    protected $insurancePerUnit = 0;
    protected $totalInsurance   = 0;
	protected $discountPerUnit  = 0;
	protected $totalDiscount    = 0;

	protected $itemType        =   '';
	protected $patientPrice    =   0;
	protected $changePriceNote =   '';
	
	// ppdong 2010-04-06: item_id (pay_payment_item_id) to update item.
	protected $itemId;
	function __construct($serviceId = '', $serviceName = '', $unit = '', $quantity = 1,
	       $modifiedPrice, $normalFee = 0, $insuranceFee = 0, $insuranceType = '', $insuranceRenumeration = 0,
	       $itemType = '') {
        $this->serviceId                =   $serviceId;
        $this->serviceName              =   $serviceName;
        $this->unit                     =   $unit;
        $this->quantity                 =   $quantity;
        $this->modifiedPricePerUnit     =   $modifiedPrice;
        $this->normalFee                =   $normalFee;
        $this->insuranceFee             =   $insuranceFee;
        $this->insuranceType            =   $insuranceType;
        $this->insuranceRenumeration    =   $insuranceRenumeration;
        $this->itemType                 =   $itemType;
    }

    // Getters
	public function getOriginalUnitPrice($localized = false){
		if ($localized) {
			return L10nHelper::renderNumber($this->originalPerUnit);
		}
		return $this->originalPerUnit;
	}

	public function getOriginalPrice($localized = false) {
		$orgPrice =   $this->originalPerUnit * $this->quantity;
        if ($localized) {
            return L10nHelper::renderNumber($orgPrice);
        }
		return $orgPrice;
	}

	public function getInsurancePrice($localized = false) {
        if ($localized) {
            return L10nHelper::renderNumber($this->totalInsurance);
        }
		return $this->totalInsurance;
	}

	public function getModifiedUnitPrice() {
		return $this->modifiedPricePerUnit;
	}

	public function getNormalFee(){
		return $this->normalFee;
	}

	public function getInsuranceFee() {
		return $this->insuranceFee;
	}

	public function getInsuranceRenumeration() {
		return $this->insuranceRenumeration;
	}

	public function getDiscount($localized = false) {
        if ($localized) {
            return L10nHelper::renderNumber($this->totalDiscount);
        }
		return $this->totalDiscount;
	}

	public function getPatientPrice($localized = false) {
        if ($localized) {
            return L10nHelper::renderNumber($this->patientPrice);
        }
		return $this->patientPrice;
	}

	public function getServiceId() {
		return $this->serviceId;
	}

    public function getServiceName() {
        return $this->serviceName;
    }

    public function getUnit() {
    	return $this->unit;
    }

    public function getQuantity() {
    	return $this->quantity;
    }

    public function getInsuranceType() {
    	return $this->insuranceType;
    }

    public function getType() {
    	return $this->itemType;
    }

    public function getChangePriceNote(){
    	return $this->changePriceNote;
    }
    
    public function getItemId() {
        return $this->itemId;
    }
    // Setters
    public function setModifiedUnitPrice($price) {
        $this->modifiedPricePerUnit   =   $price;
    }

    public function setInsurancePrice($price) {
        $this->totalInsurance   =   $price;
    }

    public function setInsuranceUnitPrice($price) {
        $this->insurancePerUnit =   $price;
    }

    public function setOriginalUnitPrice($val) {
        $this->originalPerUnit    =   $val;
    }

    public function setPatientPrice($price) {
        $this->patientPrice   =   $price;
    }

    public function setDiscount($price) {
    	$this->totalDiscount   =   $price;
    }

    public function setType($type){
        $this->itemType =   $type;
    }

    public function setChangePriceNote($val) {
    	$this->changePriceNote =   $val;
    }
    
    public function setItemId($id) {
        $this->itemId   =   $id;
    }
    
    // ppdong 2010-04-06: persist object data into pay_payment_item table
    public function update() {
        $unitPrice      =   $this->originalPerUnit;
        $originalPrice  =   $unitPrice * $this->quantity;
        $insurancePrice =   $this->totalInsurance;
        $patientPrice   =   $this->patientPrice;
        $sql    =   "UPDATE pay_payment_item SET original_price = $originalPrice,
        insurance_price = $insurancePrice, patient_price = $patientPrice
        WHERE pay_payment_item_id = " . $this->itemId;
        $db =   Celini::dbInstance();
        $db->execute($sql);
    }
}
?>